
global SSDIMed "/disk/agedisk4/medicare.work/miller-DUA50377/proj_ssdi"
cap log close 







****PURPOSE: collect estimates for MTE inference and make table 
***iterate over panels in the table (corresponding to baseline, Fig A6 panels, and male/female)
***read in "tablecolumn" that holds parameters
***print parameters for each table panel


clear
gen panel=""
foreach panel in baseline A B C D E F females males{
if !inlist("`panel'", "males", "females") append using "$SSDIMed/results/estimates/robustness/tablecolumn`column'_Panel`panel'.dta",
if inlist("`panel'", "males") append using "$SSDIMed/results/estimates/modelparameterization/tablecolumn`column'_male1.dta",
if inlist("`panel'", "females") append using "$SSDIMed/results/estimates/modelparameterization/tablecolumn`column'_male0.dta",
replace panel="`panel'" if panel==""
gen keeper=1 if inlist(var, "B_x51", "BU_x51", "B_x52", "BU_x52")
replace keeper=1 if regexm(var, "percentCF_health") 
replace keeper=1 if regexm(var, "shareBS")
keep if keeper==1 
drop sd keeper 
}
***add in checks 
/*
sort panel
gen check=. 
label var check "Verifies that this value is in relevant other CI"
foreach n in 51 52{
foreach b in lb ub{
gen mean`b'helper=`b' if var=="B_x`n'"
by panel: egen mean`b'=mean(mean`b'helper)
gen high`b'helper=`b' if var=="BU_x`n'"
by panel: egen high`b'=mean(high`b'helper)
}
replace check=(inrange(value, highlb, highub)) if var=="B_x`n'"
replace check=(inrange(value, meanlb, meanub)) if var=="BU_x`n'"
drop mean* high* 
}
tostring check, replace 
*/

***move pval to the right spot 

foreach n in 51 52{
foreach panel in baseline A B C D E F females males{
qui sum pval if var=="shareBSBU_x`n'belowB_x`n'" & panel=="`panel'"
qui replace pval=`r(mean)' if var=="B_x`n'" & panel=="`panel'"
qui sum pval if var=="shareBSB_x`n'aboveBU_x`n'" & panel=="`panel'"
qui replace pval=`r(mean)' if var=="BU_x`n'" & panel=="`panel'"
}
}
drop if regexm(var, "shareBS")
tostring pval, replace format(%5.2f) force 
*/

***make BU_x51 and BU_x52 an attribute of B_x51 and B_x52
gen altvalue=.
foreach n in 51 52{
foreach panel in baseline A B C D E F females males{
qui sum value if var=="BU_x`n'" & panel=="`panel'"
qui replace altvalue=`r(mean)' if var=="B_x`n'" & panel=="`panel'"
qui sum value if var=="BU_x`n'" & panel=="`panel'"
qui replace altvalue=`r(mean)' if var=="BU_x`n'" & panel=="`panel'"
}
}
drop if var=="BU_x51" | var=="BU_x52"
tostring altvalue, replace format(%8.0f) force


***make CIs -- need to make percents larger to accommodate rounding
rename (lb ub value) (lborig   uborig valueorig)
tostring lborig, gen(lb) format(%8.0f) force
tostring uborig, gen(ub) format(%8.0f) force
tostring valueorig, gen(value) format(%8.0f) force 
tostring lborig, gen(lb_per) format(%8.3f) force
tostring uborig, gen(ub_per) format(%8.3f) force
tostring valueorig, gen(value_per) format(%8.3f) force 
replace lb=lb_per if regexm(var, "percentCF_health")
replace ub=ub_per if regexm(var, "percentCF_health")
replace value=value_per if regexm(var, "percentCF_health")
gen CI="("+lb+", "+ub+")"
drop lb ub *orig *_per 

***reshapes to get in right format 
rename (var value CI pval altvalue) (varname varvalue varCI varpval varaltvalue)

reshape long var, i(panel varname) string 
drop if varname==""
rename (_j var) (stat value)
reshape wide value, i(panel stat) j(varname) string 
rename value* *

***get in proper order for the table
order panel stat  B_x51   B_x52  percentCF_health5000  percentCF_health1000 
replace stat="1value" if stat=="value"
replace stat="2CI" if stat=="CI"
replace stat="3altvalue" if stat=="altvalue"
replace stat="4pval" if stat=="pval"
replace panel="0baseline" if panel=="baseline"
gsort panel stat 
list
export excel using $SSDIMed/results/tables/MTEinference.xls, replace firstrow(var)


/*
*****BELOW DOES NOT WORK, SEE GITHUB FOR DISCUSSION
****NL TEST METHOD 
use "$SSDIMed/data/analysis/county-month-age_entry_sample-main.dta", clear 
keep if inrange(age_year_covstart_fill, 51, 52)
gen alpha52 = (age_year_covstart_fill == 52)
sum unemp_rate_county_atapp [aw = pop_19_61_atapp]
gen alphaUR=unemp_rate_county_atapp-`r(mean)'
gen alpha52XUR=alpha52*alphaUR
keep incidence_pop_age_atapp alpha52 alphaUR alpha52XUR pop_age_atapp fipscounty_firstnm_g county_mofd
gen entry=1
append using "$SSDIMed/data/proc/robustness_spending.dta" 
local controls_02 i.fipscounty_firstnm_g
local y incidence_pop_age_atapp
local w pop_age_atapp
local clust county_mofd
reghdfe `y' alpha52 alphaUR alpha52XUR [w=`w'], abs(`controls_02')  
est sto entry 


local clust county_mofd
local y tot_pmt 
local controls_02 i.years_since_covstart##i.fipscounty_init
rename age52 beta52
tab beta52, mi 
sum unemp_rate_county_atapp if entry==.
gen betaUR=unemp_rate_county_atapp-`r(mean)' if entry==.
gen beta52XUR=beta52*betaUR
gegen fipscounty_init=group(fipscounty_firstnm)
reghdfe `y' beta52 betaUR beta52XUR, abs(`controls_02') 
est sto spending  

suest entry spending, cluster(county_mofd) 

nlcom [entry]_b[alpha52]*[entry]_b[alphaUR]
nlcom [spending]_b[beta52]*[spending]_b[betaUR]
nlcom _b[beta52]*_b[betaUR]
nlcom _b[spending:beta52]*_b[spending:betaUR]

nlcom m: 2*[spending]_b[beta52]/[entry]_b[alpha52] 
gen double n=beta-(alpha*beta52/alpha52)
gen double mUR=2*(beta52+beta52XUR)/(alpha52+alpha52XUR) 
gen double nUR=beta+betaUR- (alpha+alphaUR)*(beta52+beta52XUR)/(alpha52+alpha52XUR) 
gen double mURminusm=2*(beta52+beta52XUR)/(alpha52+alpha52XUR) - 2*beta52/alpha52
gen double nURminusn=betaUR- (alpha+alphaUR)*(beta52+beta52XUR)/(alpha52+alpha52XUR) + (alpha*beta52/alpha52)
***elements of the figure
gen double x51=alpha 
gen double x51UR=alpha+alphaUR 
gen double x52=alpha+alpha52 
gen double x52UR=alpha+alpha52+alphaUR+alpha52XUR
gen double B_x51=n+m*x51 
gen double B_x51UR=nUR+mUR*x51UR 
gen double B_x52=n+m*x52
gen double B_x52UR=nUR+mUR*x52UR
***difference at x51 and x52
gen double BU_x51=nUR+mUR*x51
gen double diff_x51=BU_x51-B_x51
gen double BU_x52=nUR+mUR*x52
gen double diff_x52=BU_x52-B_x52


